import query from './atom/db/query';
import count from './atom/db/count';
import { IBaseTable } from './atom/db/interfaces';

declare const action_id: string;
declare const session_id: string;
declare const spaceid: string;

interface Message {
    filter: {};
    sort: {};
    page_size: number;
    page_no: number;
}

interface WagesGrantInfo extends IBaseTable {
    _id: string;
    no: string;
    staff_number: string;
    staff_name: string;
    deptid: string;
    deptname: string;
    staff_type: string;
    status: string;
    pro_no: string;
    pro_name: string;
    amount: string;
    month: string;
    grant_status: string
}


function format(date: number) {
    const _date = new Date(parseFloat(date + ''));
    let res = '';
    if (_date) {
        res = _date.getFullYear() + '-' + _format(_date.getMonth() + 1) + '-' + _format(_date.getDate());
    }
    return res;
}

function _format(d: number) {
    if (d < 10) {
        return '0' + d;
    } else {
        return d;
    }
}

async function atom(msg: Message) {
    const systemid = '6051';
    const { exec, prepare } = query(action_id, session_id, spaceid, systemid);
    const asc: string[] = [];
    const desc: string[] = [];
    if (msg.sort) {
        const key = Object.keys(msg.sort)[0];
        if (msg.sort[key] === -1) {
            desc.push(key);
        } else {
            asc.push(key);
        }
    }
    // 教职工
    prepare('wages_grant_info', ['_id', 'no', 'staff_number', 'staff_name', 'deptid', 'deptname', 'staff_type', 'status', 'pro_no', 'pro_name', 'amount', 'month', 'grant_status'], msg.filter, msg.page_size, msg.page_no, asc, desc);
    const [data] = await exec();

    // data.forEach((item: certifiInfo) => {
    // 	item['entry_party_time_str'] = item.entry_party_time ? format(item.entry_party_time) : '';
    // 	item['birthday_str'] = format(item.birthday);
    // });

    const c = count(action_id, session_id, spaceid, systemid);
    c.prepare('wages_grant_info', msg.filter);
    const [t] = await c.exec();

    return { dataArray: data, count: t };
}